package com.sample.springbatch.job.mapper;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.MappingJsonFactory;
import org.springframework.batch.item.file.LineMapper;

/**
 * Created by zwg on 2017/8/10.
 */
public class WrappedJsonLineMapper<T> implements LineMapper {
    private MappingJsonFactory factory = new MappingJsonFactory();

    private Class classz;

    public WrappedJsonLineMapper() {
    }

    public WrappedJsonLineMapper(Class classz) {
        this.classz = classz;
    }

    public T mapLine(String line, int lineNumber) throws Exception {

        if(classz==null){
            throw new Exception("WrappedJsonLineMapper mapLine Class Type is null.");
        }

        JsonParser parser = this.factory.createParser(line);
        T token = (T)parser.readValueAs(classz);
        return token;
    }

    public Class getClassz() {
        return classz;
    }

    public void setClassz(Class classz) {
        this.classz = classz;
    }
}
